home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_copy.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2005-10-18  |  35KB  |  1,079 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''Unit tests for the copy module.'''
  5. import sys
  6. import copy
  7. import copy_reg
  8. import unittest
  9. from test import test_support
  10.  
  11. class TestCopy(unittest.TestCase):
  12.     
  13.     def test_exceptions(self):
  14.         self.assert_(copy.Error is copy.error)
  15.         self.assert_(issubclass(copy.Error, Exception))
  16.  
  17.     
  18.     def test_copy_basic(self):
  19.         x = 42
  20.         y = copy.copy(x)
  21.         self.assertEqual(x, y)
  22.  
  23.     
  24.     def test_copy_copy(self):
  25.         
  26.         class C(object):
  27.             
  28.             def __init__(self, foo):
  29.                 self.foo = foo
  30.  
  31.             
  32.             def __copy__(self):
  33.                 return C(self.foo)
  34.  
  35.  
  36.         x = C(42)
  37.         y = copy.copy(x)
  38.         self.assertEqual(y.__class__, x.__class__)
  39.         self.assertEqual(y.foo, x.foo)
  40.  
  41.     
  42.     def test_copy_registry(self):
  43.         
  44.         class C(object):
  45.             
  46.             def __new__(cls, foo):
  47.                 obj = object.__new__(cls)
  48.                 obj.foo = foo
  49.                 return obj
  50.  
  51.  
  52.         
  53.         def pickle_C(obj):
  54.             return (C, (obj.foo,))
  55.  
  56.         x = C(42)
  57.         self.assertRaises(TypeError, copy.copy, x)
  58.         copy_reg.pickle(C, pickle_C, C)
  59.         y = copy.copy(x)
  60.  
  61.     
  62.     def test_copy_reduce_ex(self):
  63.         
  64.         class C(object):
  65.             
  66.             def __reduce_ex__(self, proto):
  67.                 return ''
  68.  
  69.             
  70.             def __reduce__(self):
  71.                 raise test_support.TestFailed, "shouldn't call this"
  72.  
  73.  
  74.         x = C()
  75.         y = copy.copy(x)
  76.         self.assert_(y is x)
  77.  
  78.     
  79.     def test_copy_reduce(self):
  80.         
  81.         class C(object):
  82.             
  83.             def __reduce__(self):
  84.                 return ''
  85.  
  86.  
  87.         x = C()
  88.         y = copy.copy(x)
  89.         self.assert_(y is x)
  90.  
  91.     
  92.     def test_copy_cant(self):
  93.         
  94.         class C(object):
  95.             
  96.             def __getattribute__(self, name):
  97.                 if name.startswith('__reduce'):
  98.                     raise AttributeError, name
  99.                 
  100.                 return object.__getattribute__(self, name)
  101.  
  102.  
  103.         x = C()
  104.         self.assertRaises(copy.Error, copy.copy, x)
  105.  
  106.     
  107.     def test_copy_atomic(self):
  108.         
  109.         class Classic:
  110.             pass
  111.  
  112.         
  113.         class NewStyle(object):
  114.             pass
  115.  
  116.         
  117.         def f():
  118.             pass
  119.  
  120.         tests = [
  121.             None,
  122.             42,
  123.             0x2L ** 100,
  124.             3.1400000000000001,
  125.             True,
  126.             False,
  127.             (0.0+1.0j),
  128.             'hello',
  129.             u'hello\xe1\x88\xb4',
  130.             f.func_code,
  131.             NewStyle,
  132.             xrange(10),
  133.             Classic,
  134.             max]
  135.         for x in tests:
  136.             self.assert_(copy.copy(x) is x, repr(x))
  137.         
  138.  
  139.     
  140.     def test_copy_list(self):
  141.         x = [
  142.             1,
  143.             2,
  144.             3]
  145.         self.assertEqual(copy.copy(x), x)
  146.  
  147.     
  148.     def test_copy_tuple(self):
  149.         x = (1, 2, 3)
  150.         self.assertEqual(copy.copy(x), x)
  151.  
  152.     
  153.     def test_copy_dict(self):
  154.         x = {
  155.             'foo': 1,
  156.             'bar': 2 }
  157.         self.assertEqual(copy.copy(x), x)
  158.  
  159.     
  160.     def test_copy_inst_vanilla(self):
  161.         
  162.         class C:
  163.             
  164.             def __init__(self, foo):
  165.                 self.foo = foo
  166.  
  167.             
  168.             def __cmp__(self, other):
  169.                 return cmp(self.foo, other.foo)
  170.  
  171.  
  172.         x = C(42)
  173.         self.assertEqual(copy.copy(x), x)
  174.  
  175.     
  176.     def test_copy_inst_copy(self):
  177.         
  178.         class C:
  179.             
  180.             def __init__(self, foo):
  181.                 self.foo = foo
  182.  
  183.             
  184.             def __copy__(self):
  185.                 return C(self.foo)
  186.  
  187.             
  188.             def __cmp__(self, other):
  189.                 return cmp(self.foo, other.foo)
  190.  
  191.  
  192.         x = C(42)
  193.         self.assertEqual(copy.copy(x), x)
  194.  
  195.     
  196.     def test_copy_inst_getinitargs(self):
  197.         
  198.         class C:
  199.             
  200.             def __init__(self, foo):
  201.                 self.foo = foo
  202.  
  203.             
  204.             def __getinitargs__(self):
  205.                 return (self.foo,)
  206.  
  207.             
  208.             def __cmp__(self, other):
  209.                 return cmp(self.foo, other.foo)
  210.  
  211.  
  212.         x = C(42)
  213.         self.assertEqual(copy.copy(x), x)
  214.  
  215.     
  216.     def test_copy_inst_getstate(self):
  217.         
  218.         class C:
  219.             
  220.             def __init__(self, foo):
  221.                 self.foo = foo
  222.  
  223.             
  224.             def __getstate__(self):
  225.                 return {
  226.                     'foo': self.foo }
  227.  
  228.             
  229.             def __cmp__(self, other):
  230.                 return cmp(self.foo, other.foo)
  231.  
  232.  
  233.         x = C(42)
  234.         self.assertEqual(copy.copy(x), x)
  235.  
  236.     
  237.     def test_copy_inst_setstate(self):
  238.         
  239.         class C:
  240.             
  241.             def __init__(self, foo):
  242.                 self.foo = foo
  243.  
  244.             
  245.             def __setstate__(self, state):
  246.                 self.foo = state['foo']
  247.  
  248.             
  249.             def __cmp__(self, other):
  250.                 return cmp(self.foo, other.foo)
  251.  
  252.  
  253.         x = C(42)
  254.         self.assertEqual(copy.copy(x), x)
  255.  
  256.     
  257.     def test_copy_inst_getstate_setstate(self):
  258.         
  259.         class C:
  260.             
  261.             def __init__(self, foo):
  262.                 self.foo = foo
  263.  
  264.             
  265.             def __getstate__(self):
  266.                 return self.foo
  267.  
  268.             
  269.             def __setstate__(self, state):
  270.                 self.foo = state
  271.  
  272.             
  273.             def __cmp__(self, other):
  274.                 return cmp(self.foo, other.foo)
  275.  
  276.  
  277.         x = C(42)
  278.         self.assertEqual(copy.copy(x), x)
  279.  
  280.     
  281.     def test_copy_classictype(self):
  282.         make_copyable = make_copyable
  283.         import _testcapi
  284.         x = make_copyable([
  285.             23])
  286.         y = copy.copy(x)
  287.         self.assertEqual(x, y)
  288.         self.assertEqual(x.tag, y.tag)
  289.         self.assert_(x is not y)
  290.         self.assert_(x.tag is y.tag)
  291.  
  292.     
  293.     def test_deepcopy_classictype(self):
  294.         make_copyable = make_copyable
  295.         import _testcapi
  296.         x = make_copyable([
  297.             23])
  298.         y = copy.deepcopy(x)
  299.         self.assertEqual(x, y)
  300.         self.assertEqual(x.tag, y.tag)
  301.         self.assert_(x is not y)
  302.         self.assert_(x.tag is not y.tag)
  303.  
  304.     
  305.     def test_copy_classoverinstance(self):
  306.         
  307.         class C(object):
  308.             
  309.             def __init__(self, v):
  310.                 self.v = v
  311.  
  312.             
  313.             def __cmp__(self, other):
  314.                 return -cmp(other, self.v)
  315.  
  316.             
  317.             def __copy__(self):
  318.                 return self.__class__(self.v)
  319.  
  320.  
  321.         x = C(23)
  322.         self.assertEqual(copy.copy(x), x)
  323.         
  324.         x.__copy__ = lambda : 42
  325.         self.assertEqual(copy.copy(x), x)
  326.  
  327.     
  328.     def test_deepcopy_classoverinstance(self):
  329.         
  330.         class C(object):
  331.             
  332.             def __init__(self, v):
  333.                 self.v = v
  334.  
  335.             
  336.             def __cmp__(self, other):
  337.                 return -cmp(other, self.v)
  338.  
  339.             
  340.             def __deepcopy__(self, memo):
  341.                 return self.__class__(copy.deepcopy(self.v, memo))
  342.  
  343.  
  344.         x = C(23)
  345.         self.assertEqual(copy.deepcopy(x), x)
  346.         
  347.         x.__deepcopy__ = lambda memo: 42
  348.         self.assertEqual(copy.deepcopy(x), x)
  349.  
  350.     
  351.     def test_copy_metaclassconfusion(self):
  352.         
  353.         class MyOwnError(copy.Error):
  354.             pass
  355.  
  356.         
  357.         class Meta(type):
  358.             
  359.             def __copy__(cls):
  360.                 raise MyOwnError("can't copy classes w/this metaclass")
  361.  
  362.  
  363.         
  364.         class C:
  365.             __metaclass__ = Meta
  366.             
  367.             def __init__(self, tag):
  368.                 self.tag = tag
  369.  
  370.             
  371.             def __cmp__(self, other):
  372.                 return -cmp(other, self.tag)
  373.  
  374.  
  375.         self.assertRaises(MyOwnError, copy.copy, C)
  376.         x = C(23)
  377.         self.assertEqual(copy.copy(x), x)
  378.  
  379.     
  380.     def test_deepcopy_metaclassconfusion(self):
  381.         
  382.         class MyOwnError(copy.Error):
  383.             pass
  384.  
  385.         
  386.         class Meta(type):
  387.             
  388.             def __deepcopy__(cls, memo):
  389.                 raise MyOwnError("can't deepcopy classes w/this metaclass")
  390.  
  391.  
  392.         
  393.         class C:
  394.             __metaclass__ = Meta
  395.             
  396.             def __init__(self, tag):
  397.                 self.tag = tag
  398.  
  399.             
  400.             def __cmp__(self, other):
  401.                 return -cmp(other, self.tag)
  402.  
  403.  
  404.         self.assertEqual(copy.deepcopy(C), C)
  405.         x = C(23)
  406.         self.assertEqual(copy.deepcopy(x), x)
  407.  
  408.     
  409.     def _nomro(self):
  410.         
  411.         class C(type):
  412.             
  413.             def __getattribute__(self, attr):
  414.                 if attr == '__mro__':
  415.                     raise AttributeError, 'What, *me*, a __mro__? Nevah!'
  416.                 
  417.                 return super(C, self).__getattribute__(attr)
  418.  
  419.  
  420.         
  421.         class D(object):
  422.             __metaclass__ = C
  423.  
  424.         return D()
  425.  
  426.     
  427.     def test_copy_mro(self):
  428.         x = self._nomro()
  429.         y = copy.copy(x)
  430.  
  431.     
  432.     def test_deepcopy_mro(self):
  433.         x = self._nomro()
  434.         y = copy.deepcopy(x)
  435.  
  436.     
  437.     def test_deepcopy_basic(self):
  438.         x = 42
  439.         y = copy.deepcopy(x)
  440.         self.assertEqual(y, x)
  441.  
  442.     
  443.     def test_deepcopy_memo(self):
  444.         x = []
  445.         x = [
  446.             x,
  447.             x]
  448.         y = copy.deepcopy(x)
  449.         self.assertEqual(y, x)
  450.         self.assert_(y is not x)
  451.         self.assert_(y[0] is not x[0])
  452.         self.assert_(y[0] is y[1])
  453.  
  454.     
  455.     def test_deepcopy_issubclass(self):
  456.         
  457.         class Meta(type):
  458.             pass
  459.  
  460.         
  461.         class C:
  462.             __metaclass__ = Meta
  463.  
  464.         self.assertEqual(copy.deepcopy(C), C)
  465.  
  466.     
  467.     def test_deepcopy_deepcopy(self):
  468.         
  469.         class C(object):
  470.             
  471.             def __init__(self, foo):
  472.                 self.foo = foo
  473.  
  474.             
  475.             def __deepcopy__(self, memo = None):
  476.                 return C(self.foo)
  477.  
  478.  
  479.         x = C(42)
  480.         y = copy.deepcopy(x)
  481.         self.assertEqual(y.__class__, x.__class__)
  482.         self.assertEqual(y.foo, x.foo)
  483.  
  484.     
  485.     def test_deepcopy_registry(self):
  486.         
  487.         class C(object):
  488.             
  489.             def __new__(cls, foo):
  490.                 obj = object.__new__(cls)
  491.                 obj.foo = foo
  492.                 return obj
  493.  
  494.  
  495.         
  496.         def pickle_C(obj):
  497.             return (C, (obj.foo,))
  498.  
  499.         x = C(42)
  500.         self.assertRaises(TypeError, copy.deepcopy, x)
  501.         copy_reg.pickle(C, pickle_C, C)
  502.         y = copy.deepcopy(x)
  503.  
  504.     
  505.     def test_deepcopy_reduce_ex(self):
  506.         
  507.         class C(object):
  508.             
  509.             def __reduce_ex__(self, proto):
  510.                 return ''
  511.  
  512.             
  513.             def __reduce__(self):
  514.                 raise test_support.TestFailed, "shouldn't call this"
  515.  
  516.  
  517.         x = C()
  518.         y = copy.deepcopy(x)
  519.         self.assert_(y is x)
  520.  
  521.     
  522.     def test_deepcopy_reduce(self):
  523.         
  524.         class C(object):
  525.             
  526.             def __reduce__(self):
  527.                 return ''
  528.  
  529.  
  530.         x = C()
  531.         y = copy.deepcopy(x)
  532.         self.assert_(y is x)
  533.  
  534.     
  535.     def test_deepcopy_cant(self):
  536.         
  537.         class C(object):
  538.             
  539.             def __getattribute__(self, name):
  540.                 if name.startswith('__reduce'):
  541.                     raise AttributeError, name
  542.                 
  543.                 return object.__getattribute__(self, name)
  544.  
  545.  
  546.         x = C()
  547.         self.assertRaises(copy.Error, copy.deepcopy, x)
  548.  
  549.     
  550.     def test_deepcopy_atomic(self):
  551.         
  552.         class Classic:
  553.             pass
  554.  
  555.         
  556.         class NewStyle(object):
  557.             pass
  558.  
  559.         
  560.         def f():
  561.             pass
  562.  
  563.         tests = [
  564.             None,
  565.             42,
  566.             0x2L ** 100,
  567.             3.1400000000000001,
  568.             True,
  569.             False,
  570.             (0.0+1.0j),
  571.             'hello',
  572.             u'hello\xe1\x88\xb4',
  573.             f.func_code,
  574.             NewStyle,
  575.             xrange(10),
  576.             Classic,
  577.             max]
  578.         for x in tests:
  579.             self.assert_(copy.deepcopy(x) is x, repr(x))
  580.         
  581.  
  582.     
  583.     def test_deepcopy_list(self):
  584.         x = [
  585.             [
  586.                 1,
  587.                 2],
  588.             3]
  589.         y = copy.deepcopy(x)
  590.         self.assertEqual(y, x)
  591.         self.assert_(x is not y)
  592.         self.assert_(x[0] is not y[0])
  593.  
  594.     
  595.     def test_deepcopy_reflexive_list(self):
  596.         x = []
  597.         x.append(x)
  598.         y = copy.deepcopy(x)
  599.         self.assertRaises(RuntimeError, cmp, y, x)
  600.         self.assert_(y is not x)
  601.         self.assert_(y[0] is y)
  602.         self.assertEqual(len(y), 1)
  603.  
  604.     
  605.     def test_deepcopy_tuple(self):
  606.         x = ([
  607.             1,
  608.             2], 3)
  609.         y = copy.deepcopy(x)
  610.         self.assertEqual(y, x)
  611.         self.assert_(x is not y)
  612.         self.assert_(x[0] is not y[0])
  613.  
  614.     
  615.     def test_deepcopy_reflexive_tuple(self):
  616.         x = ([],)
  617.         x[0].append(x)
  618.         y = copy.deepcopy(x)
  619.         self.assertRaises(RuntimeError, cmp, y, x)
  620.         self.assert_(y is not x)
  621.         self.assert_(y[0] is not x[0])
  622.         self.assert_(y[0][0] is y)
  623.  
  624.     
  625.     def test_deepcopy_dict(self):
  626.         x = {
  627.             'foo': [
  628.                 1,
  629.                 2],
  630.             'bar': 3 }
  631.         y = copy.deepcopy(x)
  632.         self.assertEqual(y, x)
  633.         self.assert_(x is not y)
  634.         self.assert_(x['foo'] is not y['foo'])
  635.  
  636.     
  637.     def test_deepcopy_reflexive_dict(self):
  638.         x = { }
  639.         x['foo'] = x
  640.         y = copy.deepcopy(x)
  641.         self.assertRaises(RuntimeError, cmp, y, x)
  642.         self.assert_(y is not x)
  643.         self.assert_(y['foo'] is y)
  644.         self.assertEqual(len(y), 1)
  645.  
  646.     
  647.     def test_deepcopy_keepalive(self):
  648.         memo = { }
  649.         x = 42
  650.         y = copy.deepcopy(x, memo)
  651.         self.assert_(memo[id(x)] is x)
  652.  
  653.     
  654.     def test_deepcopy_inst_vanilla(self):
  655.         
  656.         class C:
  657.             
  658.             def __init__(self, foo):
  659.                 self.foo = foo
  660.  
  661.             
  662.             def __cmp__(self, other):
  663.                 return cmp(self.foo, other.foo)
  664.  
  665.  
  666.         x = C([
  667.             42])
  668.         y = copy.deepcopy(x)
  669.         self.assertEqual(y, x)
  670.         self.assert_(y.foo is not x.foo)
  671.  
  672.     
  673.     def test_deepcopy_inst_deepcopy(self):
  674.         
  675.         class C:
  676.             
  677.             def __init__(self, foo):
  678.                 self.foo = foo
  679.  
  680.             
  681.             def __deepcopy__(self, memo):
  682.                 return C(copy.deepcopy(self.foo, memo))
  683.  
  684.             
  685.             def __cmp__(self, other):
  686.                 return cmp(self.foo, other.foo)
  687.  
  688.  
  689.         x = C([
  690.             42])
  691.         y = copy.deepcopy(x)
  692.         self.assertEqual(y, x)
  693.         self.assert_(y is not x)
  694.         self.assert_(y.foo is not x.foo)
  695.  
  696.     
  697.     def test_deepcopy_inst_getinitargs(self):
  698.         
  699.         class C:
  700.             
  701.             def __init__(self, foo):
  702.                 self.foo = foo
  703.  
  704.             
  705.             def __getinitargs__(self):
  706.                 return (self.foo,)
  707.  
  708.             
  709.             def __cmp__(self, other):
  710.                 return cmp(self.foo, other.foo)
  711.  
  712.  
  713.         x = C([
  714.             42])
  715.         y = copy.deepcopy(x)
  716.         self.assertEqual(y, x)
  717.         self.assert_(y is not x)
  718.         self.assert_(y.foo is not x.foo)
  719.  
  720.     
  721.     def test_deepcopy_inst_getstate(self):
  722.         
  723.         class C:
  724.             
  725.             def __init__(self, foo):
  726.                 self.foo = foo
  727.  
  728.             
  729.             def __getstate__(self):
  730.                 return {
  731.                     'foo': self.foo }
  732.  
  733.             
  734.             def __cmp__(self, other):
  735.                 return cmp(self.foo, other.foo)
  736.  
  737.  
  738.         x = C([
  739.             42])
  740.         y = copy.deepcopy(x)
  741.         self.assertEqual(y, x)
  742.         self.assert_(y is not x)
  743.         self.assert_(y.foo is not x.foo)
  744.  
  745.     
  746.     def test_deepcopy_inst_setstate(self):
  747.         
  748.         class C:
  749.             
  750.             def __init__(self, foo):
  751.                 self.foo = foo
  752.  
  753.             
  754.             def __setstate__(self, state):
  755.                 self.foo = state['foo']
  756.  
  757.             
  758.             def __cmp__(self, other):
  759.                 return cmp(self.foo, other.foo)
  760.  
  761.  
  762.         x = C([
  763.             42])
  764.         y = copy.deepcopy(x)
  765.         self.assertEqual(y, x)
  766.         self.assert_(y is not x)
  767.         self.assert_(y.foo is not x.foo)
  768.  
  769.     
  770.     def test_deepcopy_inst_getstate_setstate(self):
  771.         
  772.         class C:
  773.             
  774.             def __init__(self, foo):
  775.                 self.foo = foo
  776.  
  777.             
  778.             def __getstate__(self):
  779.                 return self.foo
  780.  
  781.             
  782.             def __setstate__(self, state):
  783.                 self.foo = state
  784.  
  785.             
  786.             def __cmp__(self, other):
  787.                 return cmp(self.foo, other.foo)
  788.  
  789.  
  790.         x = C([
  791.             42])
  792.         y = copy.deepcopy(x)
  793.         self.assertEqual(y, x)
  794.         self.assert_(y is not x)
  795.         self.assert_(y.foo is not x.foo)
  796.  
  797.     
  798.     def test_deepcopy_reflexive_inst(self):
  799.         
  800.         class C:
  801.             pass
  802.  
  803.         x = C()
  804.         x.foo = x
  805.         y = copy.deepcopy(x)
  806.         self.assert_(y is not x)
  807.         self.assert_(y.foo is y)
  808.  
  809.     
  810.     def test_reconstruct_string(self):
  811.         
  812.         class C(object):
  813.             
  814.             def __reduce__(self):
  815.                 return ''
  816.  
  817.  
  818.         x = C()
  819.         y = copy.copy(x)
  820.         self.assert_(y is x)
  821.         y = copy.deepcopy(x)
  822.         self.assert_(y is x)
  823.  
  824.     
  825.     def test_reconstruct_nostate(self):
  826.         
  827.         class C(object):
  828.             
  829.             def __reduce__(self):
  830.                 return (C, ())
  831.  
  832.  
  833.         x = C()
  834.         x.foo = 42
  835.         y = copy.copy(x)
  836.         self.assert_(y.__class__ is x.__class__)
  837.         y = copy.deepcopy(x)
  838.         self.assert_(y.__class__ is x.__class__)
  839.  
  840.     
  841.     def test_reconstruct_state(self):
  842.         
  843.         class C(object):
  844.             
  845.             def __reduce__(self):
  846.                 return (C, (), self.__dict__)
  847.  
  848.             
  849.             def __cmp__(self, other):
  850.                 return cmp(self.__dict__, other.__dict__)
  851.  
  852.  
  853.         x = C()
  854.         x.foo = [
  855.             42]
  856.         y = copy.copy(x)
  857.         self.assertEqual(y, x)
  858.         y = copy.deepcopy(x)
  859.         self.assertEqual(y, x)
  860.         self.assert_(y.foo is not x.foo)
  861.  
  862.     
  863.     def test_reconstruct_state_setstate(self):
  864.         
  865.         class C(object):
  866.             
  867.             def __reduce__(self):
  868.                 return (C, (), self.__dict__)
  869.  
  870.             
  871.             def __setstate__(self, state):
  872.                 self.__dict__.update(state)
  873.  
  874.             
  875.             def __cmp__(self, other):
  876.                 return cmp(self.__dict__, other.__dict__)
  877.  
  878.  
  879.         x = C()
  880.         x.foo = [
  881.             42]
  882.         y = copy.copy(x)
  883.         self.assertEqual(y, x)
  884.         y = copy.deepcopy(x)
  885.         self.assertEqual(y, x)
  886.         self.assert_(y.foo is not x.foo)
  887.  
  888.     
  889.     def test_reconstruct_reflexive(self):
  890.         
  891.         class C(object):
  892.             pass
  893.  
  894.         x = C()
  895.         x.foo = x
  896.         y = copy.deepcopy(x)
  897.         self.assert_(y is not x)
  898.         self.assert_(y.foo is y)
  899.  
  900.     
  901.     def test_reduce_4tuple(self):
  902.         
  903.         class C(list):
  904.             
  905.             def __reduce__(self):
  906.                 return (C, (), self.__dict__, iter(self))
  907.  
  908.             
  909.             def __cmp__(self, other):
  910.                 if not cmp(list(self), list(other)):
  911.                     pass
  912.                 return cmp(self.__dict__, other.__dict__)
  913.  
  914.  
  915.         x = C([
  916.             [
  917.                 1,
  918.                 2],
  919.             3])
  920.         y = copy.copy(x)
  921.         self.assertEqual(x, y)
  922.         self.assert_(x is not y)
  923.         self.assert_(x[0] is y[0])
  924.         y = copy.deepcopy(x)
  925.         self.assertEqual(x, y)
  926.         self.assert_(x is not y)
  927.         self.assert_(x[0] is not y[0])
  928.  
  929.     
  930.     def test_reduce_5tuple(self):
  931.         
  932.         class C(dict):
  933.             
  934.             def __reduce__(self):
  935.                 return (C, (), self.__dict__, None, self.iteritems())
  936.  
  937.             
  938.             def __cmp__(self, other):
  939.                 if not cmp(dict(self), list(dict)):
  940.                     pass
  941.                 return cmp(self.__dict__, other.__dict__)
  942.  
  943.  
  944.         x = C([
  945.             ('foo', [
  946.                 1,
  947.                 2]),
  948.             ('bar', 3)])
  949.         y = copy.copy(x)
  950.         self.assertEqual(x, y)
  951.         self.assert_(x is not y)
  952.         self.assert_(x['foo'] is y['foo'])
  953.         y = copy.deepcopy(x)
  954.         self.assertEqual(x, y)
  955.         self.assert_(x is not y)
  956.         self.assert_(x['foo'] is not y['foo'])
  957.  
  958.     
  959.     def test_copy_slots(self):
  960.         
  961.         class C(object):
  962.             __slots__ = [
  963.                 'foo']
  964.  
  965.         x = C()
  966.         x.foo = [
  967.             42]
  968.         y = copy.copy(x)
  969.         self.assert_(x.foo is y.foo)
  970.  
  971.     
  972.     def test_deepcopy_slots(self):
  973.         
  974.         class C(object):
  975.             __slots__ = [
  976.                 'foo']
  977.  
  978.         x = C()
  979.         x.foo = [
  980.             42]
  981.         y = copy.deepcopy(x)
  982.         self.assertEqual(x.foo, y.foo)
  983.         self.assert_(x.foo is not y.foo)
  984.  
  985.     
  986.     def test_copy_list_subclass(self):
  987.         
  988.         class C(list):
  989.             pass
  990.  
  991.         x = C([
  992.             [
  993.                 1,
  994.                 2],
  995.             3])
  996.         x.foo = [
  997.             4,
  998.             5]
  999.         y = copy.copy(x)
  1000.         self.assertEqual(list(x), list(y))
  1001.         self.assertEqual(x.foo, y.foo)
  1002.         self.assert_(x[0] is y[0])
  1003.         self.assert_(x.foo is y.foo)
  1004.  
  1005.     
  1006.     def test_deepcopy_list_subclass(self):
  1007.         
  1008.         class C(list):
  1009.             pass
  1010.  
  1011.         x = C([
  1012.             [
  1013.                 1,
  1014.                 2],
  1015.             3])
  1016.         x.foo = [
  1017.             4,
  1018.             5]
  1019.         y = copy.deepcopy(x)
  1020.         self.assertEqual(list(x), list(y))
  1021.         self.assertEqual(x.foo, y.foo)
  1022.         self.assert_(x[0] is not y[0])
  1023.         self.assert_(x.foo is not y.foo)
  1024.  
  1025.     
  1026.     def test_copy_tuple_subclass(self):
  1027.         
  1028.         class C(tuple):
  1029.             pass
  1030.  
  1031.         x = C([
  1032.             1,
  1033.             2,
  1034.             3])
  1035.         self.assertEqual(tuple(x), (1, 2, 3))
  1036.         y = copy.copy(x)
  1037.         self.assertEqual(tuple(y), (1, 2, 3))
  1038.  
  1039.     
  1040.     def test_deepcopy_tuple_subclass(self):
  1041.         
  1042.         class C(tuple):
  1043.             pass
  1044.  
  1045.         x = C([
  1046.             [
  1047.                 1,
  1048.                 2],
  1049.             3])
  1050.         self.assertEqual(tuple(x), ([
  1051.             1,
  1052.             2], 3))
  1053.         y = copy.deepcopy(x)
  1054.         self.assertEqual(tuple(y), ([
  1055.             1,
  1056.             2], 3))
  1057.         self.assert_(x is not y)
  1058.         self.assert_(x[0] is not y[0])
  1059.  
  1060.     
  1061.     def test_getstate_exc(self):
  1062.         
  1063.         class EvilState(object):
  1064.             
  1065.             def __getstate__(self):
  1066.                 raise ValueError, "ain't got no stickin' state"
  1067.  
  1068.  
  1069.         self.assertRaises(ValueError, copy.copy, EvilState())
  1070.  
  1071.  
  1072.  
  1073. def test_main():
  1074.     test_support.run_unittest(TestCopy)
  1075.  
  1076. if __name__ == '__main__':
  1077.     test_main()
  1078.  
  1079.